package com.CMS.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.CMS.client.RolService;
import com.CMS.server.dao.PrivilegeDao;
import com.CMS.server.dao.RolDao;
import com.CMS.server.domain.Privilege;
import com.CMS.server.domain.Rol;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class RpcRol extends RemoteServiceServlet implements RolService{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6284304690956288906L;

	@Override
	public List<Rol> getRoles() throws Exception {
		RolDao dao = new RolDao();
		return dao.getAll();
	}

	@Override
	public void createRol(String Description,List<Long> privilegios) {		
		RolDao dao = new RolDao();
		PrivilegeDao pdao = new PrivilegeDao();
		List<Privilege> lista = new ArrayList<Privilege>();
		Rol r = new Rol();
		r.setAddDate(new Date());
		r.setModDate(new Date());
		r.setDescription(Description);
		r.setFlagDelete(false);
		for(int i=0; i<privilegios.size(); i++){	
			lista.add(pdao.getEntity(privilegios.get(i)));
		}
		r.setPrivileges(lista);
		dao.createEntity(r);
	}

	@Override
	public void deleteRol(long id) {
		if(id > 0){
			RolDao dao = new RolDao();
			dao.deleteEntity(dao.getEntity(id));
		}
	}

	@Override
	public Rol updateRol(long id, boolean flagDelete, String description) {
		RolDao dao = new RolDao();		
		Rol r = new Rol();
		r.setId(id);
		r.setModDate(new Date());
		r.setDescription(description);
		r.setFlagDelete(flagDelete);
		return dao.updateEntity(r);
	}

	@Override
	public List<Privilege> getPrivileges(long id) {
		RolDao dao = new RolDao();
		return dao.getPrivileges(id);
	}

	@Override
	public void updatePrivileges(long idRol,List<Long> lista) {
		RolDao dao = new RolDao();
		dao.updatePrivileges(idRol, lista);
	}

}
